<?php
# Don't display PHP errors
error_reporting(0);
#include('include/config.inc.php');
include('include/header.inc.php');

# Where is the cdr0.txt
$cdrfile0 = "C:\\ProgramData\\T-Com\\cdr0.txt";
$cdrfile1 = "C:\\ProgramData\\Swyx\\cdr0.txt";
if ( file_exists($cdrfile0)){ $cdrfile = $cdrfile0; }
if ( file_exists($cdrfile1)) { $cdrfile = $cdrfile1; }

# Is there a CDR0 file?
if (!file_exists($cdrfile)) {
    echo "<br><br><h3><font color=red> $lang[thecdrfile] $cdrfile $lang[noread]!<br><br> $lang[notfound]!</font></h3>"; exit;} 

# Can we read the cdr file?
if (!is_readable($cdrfile)) {
    echo "<br><br><h3><font color=red> $lang[thecdrfile] $cdrfile $lang[noread]!<br><br> $lang[noright]?</font></h3>"; exit;} 

# Query SQLITE DB for last CallID 
$sql = "select callid from cdr order by rowid desc limit 1;";
$result=$db->query($sql);

# If there is no data in database set last timestamp to 0
$lasttimestamp = 0;
# Save data in $lasttimestamp 
if ($row = $result->fetchArray()) { $lasttimestamp = $row[0]; }

# Find new entrys in CDR0.txt

if (($handle = fopen("$cdrfile", "r")) !== FALSE) 
		{ 
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
				{
					#Name the database Datenbankfelder benennen
					list($CallId, $OriginationNumber, $OriginationName, $CalledNumber, $CalledName, $DestinationNumber, $DestinationName, $StartDate, $StartTime, $ScriptConnectDate, $ScriptConnectTime, $DeliveredDate, $DeliveredTime, $ConnectDate, $ConnectTime, $EndDate, $EndTime, $Currency, $Costs, $State, $PublicAccessPrefix, $LCRProvider, $ProjectNumber, $AOC, $OriginationDevice, $DestinationDevice, $TransferredByNumber, $TransferredByName, $TransferredCallId1, $TransferredCallId2, $TransferredToCallId, $TransferDate, $TransferTime, $DisconnectReason ) = $data;
				
					#Ist die CallId eine Nummer (Die ersten beiden Zeilen in der cdr0.txt nicht in die Datenbank schreiben)
					if (is_numeric($CallId)) 
						{
							# if there is new data in cdr0.txt.....
							if($CallId > $lasttimestamp) 
								{
								# rewrite date for database
								$Newdate = explode(".", $StartDate);
								$Tag = $Newdate[0];
								$Monat = $Newdate[1];
								$Jahr = $Newdate[2];
								$Insertdate = "$Jahr-$Monat-$Tag $StartTime";
								# ... in die Datenbank schreiben, sonst nicht				
								$db->exec("INSERT INTO cdr VALUES ('$CallId','$OriginationNumber','$OriginationName','$CalledNumber','$CalledName','$DestinationNumber','$DestinationName','$Insertdate','$StartTime','$ScriptConnectDate','$ScriptConnectTime','$DeliveredDate','$DeliveredTime','$ConnectDate','$ConnectTime','$EndDate','$EndTime','$Currency','$Costs','$State','$PublicAccessPrefix','$LCRProvider','$ProjectNumber','$AOC','$OriginationDevice','$DestinationDevice','$TransferredByNumber','$TransferredByName','$TransferredCallId1','$TransferredCallId2','$TransferredToCallId','$TransferDate','$TransferTime','$DisconnectReason')");
								#echo "$CallId, $OriginationNumber, $OriginationName, $CalledNumber, $CalledName, $DestinationNumber, $DestinationName, $Insertdate, $StartTime, $ScriptConnectDate, $ScriptConnectTime, $DeliveredDate, $DeliveredTime, $ConnectDate, $ConnectTime, $EndDate, $EndTime, $Currency, $Costs, $State, $PublicAccessPrefix, $LCRProvider, $ProjectNumber, $AOC, $OriginationDevice, $DestinationDevice, $TransferredByNumber, $TransferredByName, $TransferredCallId1, $TransferredCallId2, $TransferredToCallId, $TransferDate, $TransferTime, $DisconnectReason \n";
								#echo "$Insertdate\n";
								
								}
						}
				}
		}

# disconnect database
$db->close();
# close cdr0.txt
fclose($handle);
# how many data sets did we get?
$zahl = ($CallId - $lasttimestamp);
print "<br><br><h3>&nbsp;&nbsp;$lang[dataindb]: $lasttimestamp</h3>";
print "<h3>&nbsp;&nbsp;$lang[datain] cdr0.txt: $CallId</h3>";
# print the number of data
print("<h3>&nbsp;&nbsp;$zahl $lang[import] </h3><br><br>");
include('include/footer.inc.php');
?>